package com.io.randomaccess;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

//RandomAccessFile类似于组合使用了DataInputStream和DataOutputStream
public class UsingRandomAccessFile {

	static String file = "f:/aa.txt";

	static void display() throws IOException {
		// 以只读的方式打开文件
		RandomAccessFile rf = new RandomAccessFile(file, "r");
		for (int i = 0; i < 7; i++) {
			System.out.println(rf.readDouble());
		}
		System.out.println(rf.readUTF());
		rf.close();
	}

	public static void main(String[] args) throws IOException {
		// 以读写的方式打开
		RandomAccessFile rf = new RandomAccessFile(file, "rw");
		for (int i = 0; i < 7; i++) {
			rf.writeDouble(i * 1.414);
		}
		rf.writeUTF("哈哈哈");
		rf.close();
		display();
		rf = new RandomAccessFile(file, "rw");
		// 一个double占用8字节，查找第5个double，并修改它
		rf.seek(5 * 8);
		rf.writeDouble(47.0001);
		rf.close();
		display();
	}

}
